#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 26-Aug-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;

using Thn;
using Thn.Application;
#endregion

namespace Thn.Communication.Teleporting.Connectors.WebService
{
    /// <summary>
    /// Policy to check certificate for SSL.
    /// This policy will accept all SSL's certificate.
    /// </summary>
#if DEBUG 
    public
#else
    internal
#endif 
    class TrustAllCertificatePolicy : ICertificatePolicy
    {
        #region Fields
        /// <summary>
        /// Default validation result.
        /// </summary>
        public bool DefaultValidate = true; 
        #endregion

        #region Properties
        #endregion

        #region Check validation result
        /// <summary>
        /// Check validation result when there are problem with certificate.
        /// </summary>
        /// <param name="servicePoint">Service point.</param>
        /// <param name="certificate">Certificate.</param>
        /// <param name="problem">Problem.</param>
        /// <param name="request">Request.</param>
        /// <returns>true if validate is succeed.</returns>
        public bool CheckValidationResult(
            ServicePoint servicePoint, 
            X509Certificate certificate, 
            WebRequest request, 
            int problem)
        {
            bool ValidationResult = false;

            

            #region Tracing
            //Console.WriteLine("Certificate Problem with accessing " + request.RequestUri);
            //Console.Write("Problem code 0x{0:X8},", (int)problem);
            //Console.WriteLine(GetProblemMessage((CertificateProblem)problem));
            //Log.Warning("Certificate Problem with accessing {0}.Problem:{1}",request.RequestUri,GetProblemMessage((CertificateProblem)problem));
            #endregion

            ValidationResult = DefaultValidate;
            return ValidationResult;
        }
        #endregion

        #region get problem message
        /// <summary>
        /// Get problem message from problem.
        /// </summary>
        /// <returns></returns>
        private string GetProblemMessage(CertificateProblem problem)
        {
            string result = "";
            CertificateProblem problemList = new CertificateProblem();
            string problemMessage = Enum.GetName(problemList.GetType(), problem);
            if (problemMessage != null)
                result = "Certificate problem:" + problemMessage;
            else
                result = "Unknown Certificate Problem";
            return result;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TrustAllCertificatePolicy() 
        { }
        #endregion
    }
}
